// ui functions
var uiEnabled = true;
var _processCount = 0;
function setProcessing(processing) {
    if(processing) {
	    _processCount++
	    $("processing").className = "on";
    } else {
	    _processCount--
	    if(_processCount <= 0) {
		    _processCount = 0;
		    $("processing").className = "";
	    }
    }
    uiEnabled = (_processCount==0);
}


// form related functions
function bindCombo(select, data, propValue, propName) {
    for(var i=0; i<data.length; i++) {
		if(propValue != undefined && propName != undefined) {
			select.options[ select.options.length ] = new Option(data[i][propName], data[i][propValue]);
		} else if(data[i].text != undefined && data[i].value != undefined) {
    		select.options[ select.options.length ] = new Option(data[i].text, data[i].value);
    	} else {
        	select.options[ select.options.length ] = new Option(data[i]);
        }
    }
}

function getComboValue(select) {
	if(select.selectedIndex == -1) return "";
	var option = select.options[select.selectedIndex];
	if(option.value != undefined) {
		return option.value;
	} else {
		return option.text;
	}
}

function getComboValues(select) {
	var values = new Array();
	for(var i=0; i<select.options.length; i++) {
		var option = select.options[i];
		if(option.selected) {
			if(option.value != undefined) {
				values.push( option.value );
			} else {
				values.push( option.text );				
			}			
		}
	}
	return values;
}

function getComboText(select) {
	if(select.selectedIndex == -1) return "";
	var option = select.options[select.selectedIndex];	
	return option.text;
}

function setComboValue(select, value) {
	for(var i=0; i<select.options.length; i++) {
		if(select.options[i].value == value) {
			select.selectedIndex = i;
			return;
		}
	}
	for(var i=0; i<select.options.length; i++) {
		if(select.options[i].text == value) {
			select.selectedIndex = i;
			return;
		}
	}   		
}

// EnterPressedExecute
function EnterPressedExecute(control, controlToCall) {
	Event.observe(control, "keypress", this.control_onKeyPress.bindAsEventListener(this), false)
	this.controlToCall = controlToCall;
}
EnterPressedExecute.prototype.control_onKeyPress = function(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		if(typeof(this.controlToCall) == "function") {
			this.controlToCall();
		} else {
			this.controlToCall.focus()
			this.controlToCall.click();
		}
	}
}


// date pickers
function createDatePickers() {
	var inputDates = document.getElementsByClassName("datebox");
	for(var i=0; i<inputDates.length; i++) createDatePicker(inputDates[i]);
}

function createDatePicker(inputDate) {
	inputDate.setAttribute("autocomplete", "off");		// avoid mozilla bug
	inputDate.size = 10;
	var btnCal = document.createElement("input");
	btnCal.type = "button";
	btnCal.className = "calendarPicker";
	btnCal.value = "Pick";
	var spanCal = document.createElement("span");
	spanCal.style.position = "absolute";
	inputDate.parentNode.insertBefore(btnCal, inputDate.nextSibling);
	inputDate.parentNode.insertBefore(spanCal, inputDate.nextSibling);
	
	
	btnCal.inputDate = inputDate;
	btnCal.spanCal = spanCal;
	btnCal.onclick = btnDatePicker_click;
}

function btnDatePicker_click() {
	this.cal = new DatePicker(this.spanCal, this.inputDate, this, {format: DateUtil.simpleFormat('MM/dd/yyyy')});
	this.interval = setTimeout(datePicker_show, 100, this);
	this.onclick = null;
}

function datePicker_show(btn) {
	if(btn && btn.cal && btn.cal.show) btn.cal.show();
}

// form
function createFocusHighlights() {
	var inputs = document.getElementsByTagName("input");
	for(var i=0; i<inputs.length; i++) if(Element.hasClassName(inputs[i], "textbox")) createFocusHighlight(inputs[i]);
	inputs = document.getElementsByTagName("textarea");
	for(var i=0; i<inputs.length; i++) createFocusHighlight(inputs[i]);
	inputs = document.getElementsByTagName("select");
	for(var i=0; i<inputs.length; i++) createFocusHighlight(inputs[i]);
}

function createFocusHighlight(ctrl) {
	Event.observe(ctrl, "focus", this.control_onFocus);
	Event.observe(ctrl, "blur", this.control_onBlur);
}
function control_onFocus(e) {
	Element.addClassName(Event.element(e), "focused");
}
function control_onBlur(e) {
	Element.removeClassName(Event.element(e), "focused");
}

// misc
function toSimpleDate(date) {
	if(isNaN(date)) return {Year:1901,Month:1,Day:1};
	return {Year:date.getFullYear(), Month:date.getMonth()+1, Day:date.getDate()}; 
}
function toDate(date) {
	if(date == null || date.Year == undefined || date.Month == undefined || date.Day == undefined) return null;
	return new Date(date.Year, date.Month-1, date.Day);
}
function formatDate(date, empty) {
	if(date == null || isNaN(date) || (date.getUTCFullYear() == 1901 && date.getUTCMonth() == 0 && date.getUTCDate() == 1) ) {
		return (empty == undefined) ? "" : empty;
	} else {
		return DateUtil.simpleFormat('MM/dd/yyyy')(date);
	}
}

function formatPercentage(value) {
	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(value);
	num.setPlaces('2', false);
	num.setCurrencyValue('$');
	num.setCurrency(false);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	return num.toFormatted() + " %";
}

function formatNumber(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function formatMoney(value) {
	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(value);
	num.setPlaces('2', false);
	num.setCurrencyValue('$');
	num.setCurrency(false);
	num.setCurrencyPosition(num.LEFT_OUTSIDE);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setNegativeRed(false);
	num.setSeparators(true, ',', ',');
	return num.toFormatted();
}

function clearContents(container) {
	if(!container || container.length == 0) return;
	while(container.childNodes.length > 0) container.removeChild(container.childNodes[0]);
}

function setDocumentTitle(s) {
	window.document.title = "MDAT \\ " + s;
}

function serialize(obj, tab) {
	var s = "";
	for(var prop in obj) {
		var value = obj[prop];
		if(typeof(value) == "object") {
			s += tab + prop + ":\n" + serialize(value, tab + "  ");
		} else if(typeof(value) != "function") {
			s += tab + prop + ": " + value + "\n";
		}
	}
	return s;
}

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false; 
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; 
